<template>
    <div class="container" v-if="readyShow">
        <el-steps :active="active" align-center>
            <el-step title="债权信息"></el-step>
            <el-step title="资料上传"></el-step>
        </el-steps>
        <div style="margin-top: 8%"></div>

        <register-debts v-show="pageDebts" :form="formDebts" :showBack="showBack" @nextPage="nextPageDebts"
                        @previousPage="previousPageDebts"></register-debts>
        <register-file v-show="pageFile" :form="formFile" @nextPage="nextPageFile"
                       @previousPage="previousPageFile"></register-file>

        <el-dialog :title="repulseNote" :visible.sync="repulseVisible" width="90%" center>
            <span slot="footer" class="dialog-footer">
                <el-button @click="repulseVisible = false" type="primary" style="width: 80%">确 定</el-button>
            </span>
        </el-dialog>

        <el-dialog title="" :visible.sync="registerVisible" width="95%" center>
            <el-checkbox v-model="checked">认可通过工行短信、邮箱、公众号等送达方式</el-checkbox>
            <span slot="footer" class="dialog-footer">
                <el-button style="margin-right: 10%" type="primary" @click="registerVisible = false">取 消</el-button>
                <el-button style="margin-left: 10%" @click="toSubmit" type="primary" disabled
                           v-if="!checked">提 交</el-button>
                <el-button style="margin-left: 10%" @click="toSubmit" type="primary" v-else="!checked">提 交</el-button>
            </span>
        </el-dialog>
    </div>
</template>

<script>
    import RegisterDebts from 'components/register/register-debts.vue'
    import RegisterFile from 'components/register/register-file.vue'
    import util from 'assets/js/util.js'

    export default {
        data() {
            return {
                readyShow: false,
                registerVisible: false,
                checked: false,
                active: 0,
                pageDebts: true,
                pageFile: false,
                showBack: false,
                formDebts: {
                    creditType: '金融借贷',
                    creditNature: '',
                    mainObligor: '',
					relatedObligor:'',
                    type: '0',
                    giveFirst: '1',
                    guaranteeCond: '',
                    guaranteeForm: '0',
                    declarePrincipal: 0,
                    declareInterest: 0,
                    declareOther: 0,
                    declareTotal: 0,
                    declareProcess: '',
                    declareNote: ''
                },
                formFile: {
                    declaerFileids: ''
                },
                repulseNote: '',
                repulseVisible: false,
                caseId: 0,
                creditorId: 0
            }
        },
        mounted() {
            //this.caseId = util.getUrlParam(document.location.href, 'caseId');
            //this.creditorId = util.getUrlParam(document.location.href, 'creditorId');
            this.id = util.getUrlParam(document.location.href, 'id');

            if (util.getUrlParam(document.location.href, 'type') === 'insert') {
                this.readyShow = true;
                return
            }
            const _self = this;

            util.ajax({
                url: '/credit/info',
                params: {id: util.getUrlParam(document.location.href, 'id')},
                success: function (data) {
                    //_self.formDebts.identityType = data.data.creditDeclareFirstVO.identityType || '0';
                    _self.formDebts.creditType = _self.matchNatureText(data.data.creditDeclareFirstVO.creditType);
                    _self.formDebts.creditNature = _self.matchTypeText(data.data.creditDeclareFirstVO.creditNature);
                    // _self.formDebts.creditType = data.data.creditDeclareFirstVO.creditType;
                    // _self.formDebts.creditNature = data.data.creditDeclareFirstVO.creditNature;
                    _self.formDebts.mainObligor = data.data.creditDeclareFirstVO.mainObligor || '';
                    _self.formDebts.relatedObligor = data.data.creditDeclareFirstVO.relatedObligor || '';
                    _self.formDebts.type = data.data.creditDeclareFirstVO.type || '0';

                    if (data.data.creditDeclareFirstVO.type === 4) {
                        _self.formDebts.giveFirst = data.data.creditDeclareFirstVO.giveFirst + '';
                        // _self.formDebts.guaranteeHas = (data.data.creditDeclareFirstVO.guaranteeHas || '0') + '';
                        // _self.formDebts.guaranteeName = data.data.creditDeclareFirstVO.guaranteeName || ''
                        _self.formDebts.guaranteeCond = data.data.creditDeclareFirstVO.guaranteeCond || '';
                        _self.formDebts.guaranteeForm = data.data.creditDeclareFirstVO.guaranteeForm || '0'
                    }
                    _self.formDebts.declarePrincipal = data.data.creditDeclareFirstVO.declarePrincipal || 0;
                    _self.formDebts.declareInterest = data.data.creditDeclareFirstVO.declareInterest || 0;
                    _self.formDebts.declareOther = data.data.creditDeclareFirstVO.declareOther || 0;
                    _self.formDebts.declareTotal = data.data.creditDeclareFirstVO.declareTotal || 0;
                    _self.formDebts.declareNote = data.data.creditDeclareFirstVO.declareNote || '';
                    _self.formDebts.declareProcess = data.data.creditDeclareFirstVO.declareProcess || '';
                    _self.formFile.declaerFileids = data.data.creditDeclareFirstVO.declareFileids || '';
                    if (data.data.state === -1 && data.data.repulseNote !== null && data.data.repulseNote !== undefined) {
                        _self.repulseNote = data.data.repulseNote;
                        _self.repulseVisible = true
                    }
                    _self.readyShow = true
                },
            });
        },
        methods: {
            nextPageDebts: function (formData) {
                this.formDebts = formData;
                this.nextPage(true)
            },
            nextPageFile: function (formData) {
                this.formFile = formData;
                if (util.getUrlParam(document.location.href, 'type') === 'insert')
                    this.registerVisible = true;
                else
                    this.onSubmit();

            },
            toSubmit: function () {
                this.onSubmit();
            },
            previousPageDebts: function (formData) {
                this.formDebts = formData;
                this.nextPage(false)
            },
            previousPageFile: function (formData) {
                this.formFile = formData;
                this.nextPage(false)
            },

            nextPage: function (nextOrPrevious) {
                this.initialPage();
                if (nextOrPrevious) {
                    if (this.active++ > 1) this.active = 0
                }
                else {
                    if (this.active-- < 0) this.active = 0
                }
                switch (this.active) {
                    case 0:
                        this.pageDebts = true;
                        break;
                    case 1:
                        this.pageFile = true;
                        break
                }
            },
            initialPage: function () {
                this.pageDebts = false;
                this.pageFile = false
            },
            onSubmit: function () {
                this.formDebts.creditType = this.matchNature(this.formDebts.creditType);
                this.formDebts.creditNature = this.matchType(this.formDebts.creditNature);
                this.formDebts.guaranteeForm = this.matchGuaranteeForm(this.formDebts.guaranteeForm);

                let form = new FormData();
                if (util.getUrlParam(document.location.href, 'type') === 'update')
                    form.append('creditId', util.getUrlParam(document.location.href, 'id'));

                if (this.formDebts.creditType !== 0)
                    form.append('creditType', this.formDebts.creditType);
                if (this.formDebts.creditNature !== 0)
                    form.append('creditNature', this.formDebts.creditNature);
                form.append('identityType', this.formDebts.identityType);
                if (this.formDebts.mainObligor !== '')
                    form.append('mainObligor', this.formDebts.mainObligor);
                if (this.formDebts.relatedObligor !== '')
                    form.append('relatedObligor', this.formDebts.relatedObligor);
                if (this.formDebts.creditNature === 4) {
                    console.log(this.formDebts)
                    form.append('giveFirst', this.formDebts.giveFirst);
                    if (this.formDebts.guaranteeCond !== '')
                        form.append('guaranteeCond', this.formDebts.guaranteeCond);
                    if (this.formDebts.guaranteeForm !== '1')
                        form.append('guaranteeForm', this.formDebts.guaranteeForm)
                }

                form.append('declarePrincipal', this.formDebts.declarePrincipal);
                form.append('declareInterest', this.formDebts.declareInterest);
                form.append('declareOther', this.formDebts.declareOther);
                form.append('declareTotal', this.formDebts.declareTotal);
                form.append('declareProcess', this.formDebts.declareProcess);
                if (this.formDebts.declareNote !== '')
                    form.append('declareNote', this.formDebts.declareNote);
                form.append('declareFileids', this.formFile.declaerFileids);
                util.ajaxByForm({
                    url: "/credit/apply",
                    form: form,
                    success: function (data) {
                        if (data.status === 200) {
                            alert(util.getUrlParam(document.location.href, 'type') === 'update' ? '修改债权成功!' : '新增债权成功');
                            //window.location.href = '../index/index.html?';
                            window.location.href = '../creditor/creditor.html?';
                        }
                    }
                });
            },
            matchType: function (typeText) {
                let type = 0;
                switch (typeText) {
                    case '职工债权':
                        type = 1;
                        break;
                    case '普通债权':
                        type = 2;
                        break;
                    case '税收债权':
                        type = 3;
                        break;
                    case '优先债权':
                        type = 4;
                        break;
                    case '':
                        type = 0;
                        break;
                    default:
                        break;
                }
                return type;
            },
            matchNature: function (text) {
                let type = 0;
                switch (text) {
                    case '金融借贷':
                        type = 1;
                        break;
                    case '民间借贷':
                        type = 2;
                        break;
                    case '货款':
                        type = 3;
                        break;
                    case '其他':
                        type = 4;
                        break;
                    default:
                        break
                }
                return type
            },
            matchGuaranteeForm: function (text) {
                let type = '';
                switch (text) {
                    case '其他':
                        type = '1';
                        break;
                    case '抵押':
                        type = '2';
                        break;
                    case '质押':
                        type = '3';
                        break;
                    default:
                        break
                }
                return type
            },
            matchNatureText: function (type) {
                let typeText = '';
                switch (type) {
                    case 1:
                        typeText = '金融借贷';
                        break;
                    case 2:
                        typeText = '民间借贷';
                        break;
                    case 3:
                        typeText = '货款';
                        break;
                    case 4:
                        typeText = '其他';
                        break;
                    default:
                        break
                }
                return typeText
            },
            matchTypeText: function (type) {
                let typeText = '';
                switch (type) {
                    case 1:
                        typeText = '职工债权';
                        break;
                    case 2:
                        typeText = '普通债权';
                        break;
                    case 3:
                        typeText = '税收债权';
                        break;
                    case 4:
                        typeText = '优先债权';
                        break;
                    default:
                        break;
                }
                return typeText
            },
        },
        components: {
            RegisterDebts,
            RegisterFile
        }
    }
</script>

<style>
    .container {
        margin-top: 5%;
    }

    .el-step__main div {
        font-size: 10px;
    }
</style>
